/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.upao.util;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import java.util.List;


/**
 *
 * @author Rey
 */
public class DBInstructor {
    
    public static void save(Long id_user,String nombres, String ape_paterno, String ape_materno,
           String grado, String especialidad, String correo ) {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Transaction txn = datastore.beginTransaction();
        try {
            Entity instructor = new Entity("Instructor");
            instructor.setProperty("idUser", id_user);
            instructor.setProperty("nombres", nombres);
            instructor.setProperty("ape_paterno", ape_paterno);
            instructor.setProperty("ape_materno", ape_materno);
            instructor.setProperty("grado", grado);
            instructor.setProperty("especialidad", especialidad);
            instructor.setProperty("correo", correo);
            datastore.put(instructor);
            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
    }
    
     public static Entity getEntityInstructorByNombres(String nombre) {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Query q = new Query("Instructor");
        q.addFilter("nombres", Query.FilterOperator.EQUAL, nombre); 
        PreparedQuery pq = ds.prepare(q);
        return pq.asQueryResultIterator().next();
    }
     
     public static List<Entity> getAllInstructores(Long id_User) {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Transaction txn = ds.beginTransaction();
        Query q = new Query("Instructor");
        q.addFilter("id_user", Query.FilterOperator.EQUAL, id_User); 
        PreparedQuery pq;
        try {
            pq = ds.prepare(q);
            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
        return pq.asList(FetchOptions.Builder.withLimit(10));
    }
    public static List<Entity> getInstructores() {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Transaction txn = ds.beginTransaction();
        Query q = new Query("Instructor");
        //q.addFilter("id_user", Query.FilterOperator.EQUAL, id_User); 
        PreparedQuery pq;
        try {
            pq = ds.prepare(q);
            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
        return pq.asList(FetchOptions.Builder.withLimit(10));
    }
    
     public static void deleteInstructorById(Long id) {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Transaction txn = datastore.beginTransaction();
        try {
            Key key = KeyFactory.createKey("Instructor", id);
            datastore.delete(key);
            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
    }
    
    
     
     
}
